<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 新闻分类
 *
 * @author www.dingwei.cn
 */
class ConsumerFindCategoryModel extends MY_Model{

    private $tbFindCategory = 'consumer_find_category';

    /**
     * 获取列表数据
     * @return 	array
     * @author heyu
     */
    public function adminPcGetList(){
        $this->db->order_by('sort desc');
        $data = $this->getAll($this->tbFindCategory);
        return $data;
    }

    /**
     * 获取分类列表
     * @param array $ids [收藏使用];
     * @author zyl
     */
    public function getList($ids = array()){
    	$this->db->select('id,attr_cate_id,name')
    	->order_by('sort asc');
    	return $this->getAll($this->tbFindCategory);
    }
    
    /**
     * 获取分类详情
     * @param $id
     * @return array
     * @author heyu
     */
    public function getInfo($id){
        if (!$id){
            return array();
        }
        $this->db->where(array('id' => $id));
        $info = $this->getOne($this->tbFindCategory);
        if (isset($info['icon'])){
            $info['icon'] = base_url($info['icon']);
        }
        return $info;
    }

    /**
     * 保存分类
     * @param 	array 	$data
     * @param   integer $id
     * @return	boolean|integer
     * @author heyu
     */
    public function save($data, $id = 0){
        if($id){
            $this->db->where('id', $id);
            return $this->update($this->tbFindCategory, $data);
        }

        return $this->insert($this->tbFindCategory, $data);
    }

    /**
     * 批量保存
     * @param $data
     * @param $key
     * @return bool
     */
    public function saveBatch($data, $key){
        if ($key){
            return $this->updateBatch($this->tbFindCategory, $data, $key);
        }
        return $this->insertBatch($this->tbFindCategory, $data);
    }

    /**
     * 获取最大排序号
     * @return int
     * @author heyu
     */
    public function getMaxSort(){
        $this->db->select('max(sort) as sort');
        $tA = $this->getOne($this->tbFindCategory);
        if (!$tA){
            return 1;
        }
        return $tA['sort']+1;
    }

    /**
     * 批量查询分类信息
     * @param $ids
     * @return array
     * @author heyu
     */
    public function getInfoByIds($ids){
        $this->db->select('id,type_name')
            ->where_in('id', $ids);
        $tA = $this->getAll($this->tbFindCategory);
        $data = array();
        foreach ($tA as $v){
            $data[$v['id']] = $v;
        }
        unset($tA);
        return $data;
    }

    /**
     * 删除
     * @param $id
     * @return bool
     * @author heyu
     */
    public function deleteNewsType($id){
        if (!$id){
            return false;
        }
        $this->db->where(array('id' => $id));
        return $this->delete($this->tbFindCategory);
    }

}